package com.yuan.zs.ui;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yuan.base.framework.BaseFrameworkFragment;
import com.yuan.base.framework.BaseViewHolderAdapter;
import com.yuan.base.framework.http.BaseHttpCallBack;
import com.yuan.base.framework.http.BaseHttpRequest;
import com.yuan.base.widget.AutoLoadList;
import com.yuan.zs.R;
import com.yuan.zs.Urls;
import com.yuan.zs.bean.Live;
import com.yuan.zs.bean.School;
import com.yuan.zs.common.CacheManager;

import java.util.HashMap;
import java.util.List;

/**
 * 直播
 */
public class MainVideoFragment extends BaseFrameworkFragment {

	// --------------------------控件--------------------------
	private AutoLoadList auto_load_list;
	private ListView data_list;// 下拉刷新
	// --------------------------变量--------------------------
	private List<Live> dataList;
	private LiveAdapter liveAdapter;
	private int index = 0;

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_main_video;
	}

	@Override
	protected void bindViews() {
		auto_load_list = findViewById(R.id.auto_load_list);
		data_list = auto_load_list.getData_list();
		auto_load_list.setNeedShowFooter(false);
		auto_load_list.setOnRefreshingListener(new AutoLoadList.OnRefreshingListener() {
			@Override
			public void onRefresh() {
				getListData(AutoLoadList.OPT_REFREASH);
			}

			@Override
			public void onLoadMore() {
				getListData(AutoLoadList.OPT_LOADMORE);
			}
		});

		data_list.setOnItemClickListener((parent, view, position, id) -> {
			Intent intent = new Intent(context, VideoDetailActivity.class);
			intent.putExtra("video", (Live) parent.getItemAtPosition(position));
			startActivity(intent);
		});
	}

	@Override
	protected void initUI() {
		auto_load_list.startRefreshing();
	}

	private void getListData(final int opt) {
		if (opt == AutoLoadList.OPT_INIT || opt == AutoLoadList.OPT_REFREASH) {
			index = 0;
		}
		if (opt == AutoLoadList.OPT_LOADMORE) {
			index++;
		}

		HashMap<String, Object> params = new HashMap<>();
		params.put("page", index);
		params.put("size", auto_load_list.getRowCount());
		BaseHttpRequest.get(Urls.getUrl(Urls.getLiveVideo), params, new BaseHttpCallBack<JSONObject>() {

			@Override
			public void onSuccess(JSONObject result) {
				if (result != null && result.containsKey("content")) {
					List<Live> resultList = JSON.parseArray(result.getString("content"), Live.class);
					switch (opt) {
						case AutoLoadList.OPT_INIT:
						case AutoLoadList.OPT_REFREASH:
							dataList = resultList;
							init(true);
							break;
						case AutoLoadList.OPT_LOADMORE:
							dataList.addAll(resultList);
							init(false);
							break;
					}
				}
			}

			@Override
			public void onPost() {
				auto_load_list.completeRefresh();
			}
		});

	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
	}

	@Override
	public void onResume() {
		super.onResume();
	}


	@Override
	public void onStart() {
		super.onStart();
	}

	private void init(boolean needNew) {
		if (needNew) {
			liveAdapter = new LiveAdapter(context);
			liveAdapter.setData(dataList);
			data_list.setAdapter(liveAdapter);
		} else {
			liveAdapter.setData(dataList);
			liveAdapter.notifyDataSetChanged();
		}
	}

	private class LiveAdapter extends BaseViewHolderAdapter<Live, Holder> {

		private LiveAdapter(Context context) {
			super(context);
		}

		@Override
		protected void bindViews(View convertView, Holder holder, Live item) {
			holder.tv_live_title = convertView.findViewById(R.id.tv_live_title);
			holder.tv_live_content = convertView.findViewById(R.id.tv_live_content);
			holder.tv_school_name = convertView.findViewById(R.id.tv_school_name);
		}

		@Override
		protected View getConvertView(Live item, LayoutInflater inflater, int position, ViewGroup parent) {
			return inflater.inflate(R.layout.item_live_list, parent, false);
		}

		@Override
		protected Holder getHolder() {
			return new Holder();
		}

		@Override
		protected void refreshView(int position, Live item, View convertView, Holder holder) {
			holder.tv_live_title.setText(item.getTitle());
			holder.tv_live_content.setText(item.getContent());
			CacheManager.getInstance().getSchool(item.getSchoolId(), new CacheManager.HandleCallback<School>() {
				@Override
				public void onCacheSuccess(School result) {
					holder.tv_school_name.setText(result.getName());
				}

				@Override
				public void onHttpSuccess(School result) {
					holder.tv_school_name.setText(result.getName());
				}

				@Override
				public void onError() {
				}
			});
		}
	}

	private class Holder {
		TextView tv_live_title;
		TextView tv_live_content;
		TextView tv_school_name;
	}
}
